package net.wds.study.teacher.dao.impl;/**
 * Package: net.wds.study.teacher.dao.impl
 * Description： TODO
 * Author: 作者
 * Date: Created in 2020/9/9 9:26
 * Company: 公司
 * Version: 0.0.1
 */

import net.wds.study.student.modal.PageModal;
import net.wds.study.teacher.dao.TeacherDao;
import net.wds.study.teacher.entity.Teacher;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : TeacherDaoImpl
 * @Description :
 * @Author : CXH
 * @Date: 2020-09-09 09:26
 */
@Repository("teacherDao")
public class TeacherDaoImpl implements TeacherDao {

    @Resource
    private SessionFactory sf;

    /*   获取当前Session*/
    public Session getSession() {
        return sf.getCurrentSession();
    }

    public SessionFactory getSf() {
        return sf;
    }

    /**
     * 方法实现说明  通过老师的id号查询老师
     *
     * @return
     * @throws
     * @author cxh
     * @date 2020/9/11 14:25
     */
    @Override
    public Teacher findTeacherById(Integer id) {
        Teacher teacher = (Teacher) getSession().get(Teacher.class, id);
        return teacher;
    }

    /**
     * 方法实现说明  通过id号对教师删除
     *
     * @return
     * @throws
     * @author cxh
     * @date 2020/9/11 14:28
     */
    @Override
    public Boolean deleteTeacherById(Integer id) {
        try {
            String hql = "delete Teacher where id="+id;
            getSession().createQuery(hql).executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 方法实现说明  更新教师信息的方法
     *
     * @return
     * @throws
     * @author cxh
     * @date 2020/9/11 14:28
     */
    @Override
    public boolean updateTeacher(Teacher teacher) {
        StringBuilder hql = new StringBuilder("update Teacher set ");
        hql.append(" teacherNum='" + teacher.getTeacherNum() + "',");
        hql.append(" teacherName='" + teacher.getTeacherName() + "',");
        hql.append(" teacherSex=" + teacher.getTeacherSex() + "," );
        hql.append("post='" + teacher.getPost() + "'");
        hql.append(" where id=" + teacher.getId() );
        Query query = getSession().createQuery(hql.toString());
        return query.executeUpdate() > 0 ? true : false;
    }

    /**
     * 方法实现说明 增加教师
     *
     * @return
     * @throws
     * @author cxh
     * @date 2020/9/11 14:30
     */
    @Override
    public Serializable addTeacher(Teacher teacher) {
        Serializable save = getSession().save(teacher);
        return save != null ? true : false;
    }

    /**
     * 方法实现说明 进行分页查询
     *
     * @return
     * @throws
     * @author cxh
     * @date 2020/9/10 20:38
     */

    public PageModal<Teacher> getPageByParam(int startIndex, Integer pageSize, String name, Integer sex) {
        PageModal<Teacher> pageModal = new PageModal<Teacher>();
        if (pageSize != null && pageSize > 0) {
            pageModal.setPageSize(pageSize);
        }
        List<Object> params = new ArrayList<Object>();
        StringBuilder hql = new StringBuilder("select t from Teacher  t  where 1=1 ");
        if (name != null && !("".equals(name.trim()))) {
            System.out.println("满足条件");
            hql.append(" and t.teacherName like ?");
            params.add("%" + name.trim() + "%");
        }
        if (sex != null && (sex == 1 || sex == 0)) {
            hql.append(" and t.teacherSex=? ");
            params.add(sex);
        } else {
            System.out.println("不满足条件");
        }
        Query query = getSession().createQuery(hql.toString());
        System.out.println(query);
        for (int i = 0; i < params.size(); i++) {
            query.setParameter(i, params.get(i));
        }
        int totalPageNum = query.list().size();//总条数
        pageModal.setTotal(totalPageNum);
        //计算为总页数
        totalPageNum = totalPageNum % pageModal.getPageSize() == 0 ? totalPageNum / pageModal.getPageSize() : (totalPageNum / pageModal.getPageSize() + 1);
        pageModal.setTotalPageNum(totalPageNum);

        //添加分页信息
        query.setFirstResult(startIndex);
        query.setMaxResults(pageModal.getPageSize());

        List<Teacher> list = query.list();
        pageModal.setPageData(list);
        return pageModal;
    }
}
